﻿using Microsoft.Extensions.Options;
using Secs4Net;
using System;

namespace PXFAP.SecsNetWinApplication
{
    internal class Equipment
    {
        public static Equipment Configure(Action<Equipment> action)
        {
            if (action == null)
                throw new ArgumentNullException(nameof(action));

            var eq = new Equipment();
            action(eq);
            return eq;
        }
        public ushort EquipmentId { get; set; }
        public string Host { get; set; }
        public int Port { get; set; }
        public string State { get; set; }
        /// <summary>
        /// 连接模式 
        /// 主动 / 被动
        /// </summary>
        public string Mode { get; set; }
        public int SocketReceiveBufferSize { get; set; }
        /// <summary>
        /// 设备 断开连接
        /// </summary>
        public Action<Action> OnDispose { get; set; }

        /// <summary>
        /// 保存 SecsGem 对象  
        /// </summary>
        public SecsGem SecsGem { get; set; }

        public IOptions<SecsGemOptions> GetSecsGemOptions()
        { 
            return Options.Create(new SecsGemOptions
            {
                IsActive = Mode == "主动" ? true : false,
                IpAddress = Host,
                Port = Port,
                SocketReceiveBufferSize = SocketReceiveBufferSize,
                DeviceId = EquipmentId,
            });
        }
    }
}
